import os.path
import time
from selenium.webdriver.common.by import By
# from selenium.common.exceptions import TimeoutException
from common.base import Base
from common.functions import open_browser



from page.login_page import LoginPage


class ShippingAddressPage(Base):
    login_url = 'http://139.129.26.163/user.php'
    # 定位收货地址
    def address(self):
        time.sleep(2)
        self.find_ele((By.XPATH, '//div/a[4]')).click()
    # 定位省的下拉框
    def province(self, locator, value):
        time.sleep(2)
        self.select_by_value(locator, value)

    # 定位市
    def city(self, locator, value):
        time.sleep(2)
        self.select_by_value(locator, value)
        time.sleep(2)

    # 定位区
    def urban(self, locator, value):
        time.sleep(2)
        self.select_by_value(locator, value)

    # 收货人姓名
    def consignee_name(self, name):
        time.sleep(2)
        self.find_ele((By.ID, 'consignee_0')).send_keys(name)
    # 详细地址
    def complete_address(self, address):
        time.sleep(2)
        self.send_keys((By.ID, 'address_0'), address)
    # 电话
    def mobil_phone(self, phone):
        time.sleep(2)
        self.send_keys((By.ID, 'tel_0'), phone)

    # 邮件地址
    def email(self, email):
        time.sleep(2)
        self.clear((By.ID, 'email_0'))
        time.sleep(2)
        self.send_keys((By.ID, 'email_0'), email)
        time.sleep(2)

    # 邮政编码
    def postal_code(self, code):
        time.sleep(2)
        self.send_keys((By.ID, 'zipcode_0'), code)

    # 手机
    def tel(self, tel):
        time.sleep(2)
        self.send_keys((By.ID, 'mobile_0'), tel)

    # 新增收货地址
    def new_address(self):
        time.sleep(4)
        self.find_ele((By.XPATH, '//input[@value="新增收货地址"]')).click()


    # 修改 姓名
    def modify(self, name):
        time.sleep(2)
        self.clear((By.ID, 'consignee_0'))
        time.sleep(2)
        self.consignee_name(name)
        time.sleep(2)

    # 点击确认修改
    def revise(self):
        time.sleep(5)
        self.find_ele((By.XPATH, '//input[@class="bnt_blue_1"]')).click()

    # 删除
    def delete(self):
        time.sleep(2)
        self.find_ele((By.CLASS_NAME, 'bnt_blue')).click()
        time.sleep(3)



if __name__ == '__main__':
    driver = open_browser()  # 调用functions.py打开浏览器
    login = LoginPage(driver)
    s = ShippingAddressPage(driver)  # 实例化对象传入浏览器参数
    s.get('http://139.129.26.163/user.php')  # 获取登录的网址
    login.input_username('ifmt2')  # 登录输入框
    login.input_password('a123456')  # 密码框
    login.btn_login()  # 登录
    s.address()  # 定位收货地址
    s.province((By.ID, 'selProvinces_0'), '18')  # 定位湖北省的下拉框
    s.city((By.ID, 'selCities_0'), '204')    # 定位武汉市的下拉框
    s.urban((By.ID, 'selDistricts_0'), '2137')  # 定位硚口区的下拉框
    s.consignee_name('小乔')  # 姓名
    s.complete_address('湖北省武汉市三国镇') # 地址
    s.mobil_phone('123456789')  # 电话
    s.email('311@qq.com')  # 邮件
    s.postal_code('1234')  # 邮编
    s.tel('12345678')  # 手机
    s.new_address()  # 新增收货地址
    s.modify('大乔') # 修改姓名
    s.revise()
    s.delete()
    s.alert_accpet()
    s.quit(3)
